{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 09 盛菜装盘-结果导出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 一个cell输出多行语句\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、导出为.xlsx文件\n",
    "**to_excel()**方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户ID</th>\n",
       "      <th>客户分类</th>\n",
       "      <th>区域</th>\n",
       "      <th>是否省会</th>\n",
       "      <th>7月销量</th>\n",
       "      <th>8月销量</th>\n",
       "      <th>9月销量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>59224</td>\n",
       "      <td>A类</td>\n",
       "      <td>一线城市</td>\n",
       "      <td>是</td>\n",
       "      <td>6</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>55295</td>\n",
       "      <td>B类</td>\n",
       "      <td>三线城市</td>\n",
       "      <td>否</td>\n",
       "      <td>37</td>\n",
       "      <td>27</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>46035</td>\n",
       "      <td>A类</td>\n",
       "      <td>二线城市</td>\n",
       "      <td>是</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2459</td>\n",
       "      <td>C类</td>\n",
       "      <td>一线城市</td>\n",
       "      <td>是</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>22179</td>\n",
       "      <td>B类</td>\n",
       "      <td>三线城市</td>\n",
       "      <td>否</td>\n",
       "      <td>9</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>22557</td>\n",
       "      <td>A类</td>\n",
       "      <td>二线城市</td>\n",
       "      <td>是</td>\n",
       "      <td>42</td>\n",
       "      <td>20</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    用户ID 客户分类    区域 是否省会  7月销量  8月销量  9月销量\n",
       "0  59224   A类  一线城市    是     6    20     0\n",
       "1  55295   B类  三线城市    否    37    27    35\n",
       "2  46035   A类  二线城市    是     8     1     8\n",
       "3   2459   C类  一线城市    是     7     8    14\n",
       "4  22179   B类  三线城市    否     9    12     4\n",
       "5  22557   A类  二线城市    是    42    20    55"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('./data/train-pivot.csv', encoding='gbk')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 设置文件导出路径\n",
    "**excel_writer**参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer=r'./data/excel_export.xlsx')\n",
    "# 注意：如果同一导出文件已经打开，再次导出会报错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 设置sheet名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer=r'./data/excel_export.xlsx', sheet_name='测试文档')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 设置索引\n",
    "**index=False**导出是去掉索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer=r'./data/excel_export.xlsx', sheet_name='测试文档', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4 设置要导出的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer=r'./data/excel_export.xlsx', sheet_name='测试文档', index=False, \n",
    "            columns=['用户ID', '7月销量', '8月销量', '9月销量'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.5 设置文件编码格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer=r'./data/excel_export.xlsx', sheet_name='测试文档', index=False, \n",
    "            columns=['用户ID', '7月销量', '8月销量', '9月销量'], encoding='utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.6 缺失值的处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer=r'./data/excel_export.xlsx', sheet_name='测试文档', index=False, \n",
    "            columns=['用户ID', '7月销量', '8月销量', '9月销量'], encoding='utf-8', na_rep=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.7 无穷值的处理\n",
    "当一个浮点数除以0时，就会得到一个无穷值：正无穷值和负无穷值\n",
    "\n",
    "float('inf')和float('-inf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer=r'./data/excel_export.xlsx', sheet_name='测试文档', index=False, \n",
    "            columns=['用户ID', '7月销量', '8月销量', '9月销量'], encoding='utf-8', na_rep=0, inf_rep=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、导出为.csv文件\n",
    "**to_csv()**方法\n",
    "\n",
    "### 2.1 设置文件导出路径\n",
    "**path_or_buf**参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf=r'./data/csv_export.csv')\n",
    "# 注意：如果同一导出文件已经打开，再次导出会报错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 设置索引\n",
    "**index=False**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf=r'./data/csv_export.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 设置要导出的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf=r'./data/csv_export.csv', index=False, columns=['用户ID', '7月销量', '8月销量', '9月销量'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.4 设置分隔符号\n",
    "**seq**参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf=r'./data/csv_export.csv', index=False, columns=['用户ID', '7月销量', '8月销量', '9月销量'], sep=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.5 缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf=r'./data/csv_export.csv', index=False, columns=['用户ID', '7月销量', '8月销量', '9月销量'], sep=',', na_rep=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6 设置编码格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf=r'./data/csv_export.csv', index=False, columns=['用户ID', '7月销量', '8月销量', '9月销量'], sep=',', na_rep=0,\n",
    "         encoding='utf-8-sig')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、将文件导出到多个Sheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = pd.DataFrame([['小张', 100, 650], ['小王', 101, 600], ['小李', 102, 578]],\n",
    "                   columns=['姓名', '学号', 'f_成绩'])\n",
    "df2 = pd.DataFrame([[100, 586], [100, 602], [101, 691], [101, 702], [102, 645], [102, 676]],\n",
    "                   columns=['学号', 'e_成绩'])\n",
    "df3 = pd.merge(df1, df2)\n",
    "# 声明一个读写对象\n",
    "# excelpath为文件要存放的路径\n",
    "excelpath = r'./data/excel_writer.xlsx'\n",
    "writer = pd.ExcelWriter(excelpath, engine='xlsxwriter')\n",
    "# 分别将表df1,df2,df3写入Excel中的Sheet1，Sheet2，Sheet3\n",
    "df1.to_excel(writer, sheet_name='表1')\n",
    "df2.to_excel(writer, sheet_name='表2')\n",
    "df3.to_excel(writer, sheet_name='表3')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
